package com.rzjm.exercise.module.controller;

import com.rzjm.exercise.common.base.controller.BaseController;
import com.rzjm.exercise.common.base.data.DataTableLayUI;
import com.rzjm.exercise.common.log.annotation.Log;
import com.rzjm.exercise.common.utils.R;
import com.rzjm.exercise.module.domain.Towns;
import com.rzjm.exercise.module.service.TownsService;
import com.rzjm.exercise.sys.system.user.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

/**
 * 乡镇村居名称列表
 *
 * @author mr.zhou
 * @email zhouwei_nice@163.com
 * @date 2020-12-15 22:36:23
 */

@Controller
@RequestMapping("/module/town")
public class TownController extends BaseController {
    @Resource
    private TownsService townsService;

    @Log("查询乡镇村居页面")
    @GetMapping("/{parentId}")
    @RequiresPermissions("module:town:town")
    String Towns(Model model,@PathVariable("parentId") Integer parentId) {
        model.addAttribute("parentId",parentId);
        UserDO user = getUser();
//        if(user.getUserType() == 7 || user.getUserType() == 8 || user.getUserType() == 9){
//            model.addAttribute("parentId",user.getTownId());
//        }
        model.addAttribute("userType",user.getUserType());
        return "module/town/town";
    }

    @Log("查询乡镇村居")
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("module:town:town")
    public DataTableLayUI<Towns> list(DataTableLayUI<Towns> condition) {
        Map<String,Object> map = condition.getSearchParams();
        UserDO user = getUser();
        int parentId = Integer.parseInt(map.get("search_eq_parentId").toString());
        if(parentId == 0){
            if(user.getUserType() == 7 || user.getUserType() == 9){
                map.put("search_eq_id",user.getTownId());
                condition.setSearchParams(map);
            }
        }
        return townsService.pageSearchLayUI(condition);
    }

    @Log("新增乡镇村居页面")
    @GetMapping("/add/{parentId}")
    @RequiresPermissions("module:town:add")
    String add(Model model,@PathVariable("parentId") Integer parentId) {
        Towns towns = new Towns();
        UserDO user = getUser();
        model.addAttribute("userType", user.getUserType());
        towns.setParentId(parentId);
        model.addAttribute("town", towns);
        return "module/town/detail";
    }

    @Log("编辑乡镇村居页面")
    @GetMapping("/edit/{id}")
    @RequiresPermissions("module:town:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        Towns towns = townsService.findById(id);
        UserDO user = getUser();
        model.addAttribute("userType", user.getUserType());
        model.addAttribute("town", towns);
        return "module/town/detail";
    }

    /**
     * 保存
     */
    @Log("新增乡镇村居")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("module:town:add")
    public R save(@Valid Towns town) {
        if(town.getParentId() > 0){
            town.setGridLevel(3);
        }else{
            town.setGridLevel(2);
        }
        town.setState(1);
        town.setDelFlag(0);
        if (townsService.save(town)) {
            return R.ok();
        }
        return R.error();
    }


    /**
     * 修改
     */
    @Log("修改乡镇村居")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("module:town:edit")
    public R update(@Valid Towns town) {
        if(town.getParentId() > 0){
            town.setGridLevel(3);
        }else{
            town.setGridLevel(2);
        }
        town.setState(1);
        town.setDelFlag(0);
        townsService.updateById(town);
        return R.ok();
    }
}
